#include "stm32f0xx.h"
#include "stm32f0xx_tim.h"
#include "stm32f0xx_rcc.h"
#include "PWM.h"
#include "led.h"
void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef	TIM_TimeBaseStructure;
  TIM_OCInitTypeDef	TIM_OCInitStructure;

  /* GPIOA Clocks enable */
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE); 
  /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);  
  /* TIM1 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
  
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);//PA6

  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);//PA7
  
  TIM_OCInitStructure.TIM_Pulse = 0;
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);//PB1

  /* TIM1 counter enable */
  TIM_Cmd(TIM3, ENABLE);

  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM3, ENABLE);

}

#define BREATHE_UP		0
#define BREATHE_DOWN		1
#define	LED_LIGHTENESS		0x1FFF
void LED_SET( unsigned int LED_CMD, unsigned int LED_state)
{
	static float breatn_factor = 1;   
	static int breath_up_down = 0,flash_cnt = 0;
	int temp;
	float breathe_para;

	if(LED_state ==LED_flash)
	{
		flash_cnt ++;
		temp = (flash_cnt/500)%2;
		if(temp == 1)
		{
			breatn_factor = 1; 
		}
		else
		{
			breatn_factor = 0; 
		}
	}
	else if(LED_state ==LED_breathe)
	{
		if(breath_up_down == BREATHE_UP)
		{
			breatn_factor +=0.001; 
				if(breatn_factor>=1)
				{
					breath_up_down = BREATHE_DOWN;
				}
		}
		else
		{
			breatn_factor -=0.001; 
			if(breatn_factor<=-0.3)
			{
				breath_up_down = BREATHE_UP;
			}
		}
	}
	else
	{
		breatn_factor = 1; 
	}

	breathe_para = breatn_factor;
	if(breathe_para<0)
	{
		breathe_para = 0;
	}

	
	if(LED_CMD==LED_GREEN)
	{
		TIM_SetCompare2(TIM3,(uint32_t)(LED_LIGHTENESS*breathe_para));
		TIM_SetCompare1(TIM3,0x00);
		TIM_SetCompare4(TIM3,0x00);	
	}
	else if(LED_CMD==LED_BLUE)
	{
		TIM_SetCompare1(TIM3,(uint32_t)(LED_LIGHTENESS*breathe_para));//PA6
		TIM_SetCompare2(TIM3,0x00);						//PA7
		TIM_SetCompare4(TIM3,0x00);						//PB1	
	}
	else if(LED_CMD==LED_RED)
	{
		TIM_SetCompare4(TIM3,(uint32_t)(LED_LIGHTENESS*breathe_para));
		TIM_SetCompare1(TIM3,0x00);
		TIM_SetCompare2(TIM3,0x00);
	}
	else if(LED_CMD==LED_yellow)
	{
		TIM_SetCompare4(TIM3,(uint32_t)(LED_LIGHTENESS*breathe_para*1.3));
		TIM_SetCompare2(TIM3,(uint32_t)(LED_LIGHTENESS*breathe_para*0.5));
		TIM_SetCompare1(TIM3,0x00);
	}
	else
	{
		TIM_SetCompare1(TIM3,0x00);
		TIM_SetCompare2(TIM3,0x00);
		TIM_SetCompare4(TIM3,0x00);	
	}
}

